﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生选课及成绩查询管理系统
{
    public partial class addTeacherInfo : Form
    {
        public addTeacherInfo()
        {
            InitializeComponent();
        }

        private void btn_sub_Click(object sender, EventArgs e)
        {
            string tname = txt_tname.Text;
            string tgender = txt_tgender.Text;
            string tage = txt_tage.Text;
            if (string.IsNullOrEmpty(tname) || string.IsNullOrEmpty(tgender) || string.IsNullOrEmpty(tage))
            {
                MessageBox.Show("请输入完整信息");
                return;
            }
            int flag = -1;
            if (int.TryParse(tgender, out flag))
            {
                if (flag == 0 || flag == 1)
                {

                }
                else
                {
                    MessageBox.Show("请输入0或1代表性别！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请输入0或1代表性别！");
                return;
            }
            if (int.TryParse(tage, out flag))
            {
                if (flag < 0 || flag > 150)
                {
                    MessageBox.Show("请输入0-150之间的整型数值！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请输入0-150之间的整型数值！");
                return;
            }

            string sql = "insert into teacher(tname,tgender,tage) values(@tname,@tgender,@tage)";
            SqlParameter[] sqlarray = { new SqlParameter("@tname",tname),
                                          new SqlParameter("@tgender",tgender),new SqlParameter("@tage",tage)};
            int res = DBAccess.GetState(sql, sqlarray);
            if (res != 0)
            {
                MessageBox.Show("成功添加老师信息");
                addTeacherInfo_Load(sender,e);
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addTeacherInfo_Load(object sender, EventArgs e)
        {
            string sql = "select * from teacher";
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = dataGridView1.CurrentCellAddress.Y + 1;
            int col = dataGridView1.CurrentCellAddress.X + 1;
            txt_tname.Text = dataGridView1.Rows[row - 1].Cells[1].Value.ToString();
            txt_tgender.Text = dataGridView1.Rows[row - 1].Cells[2].Value.ToString();
            txt_tage.Text = dataGridView1.Rows[row - 1].Cells[3].Value.ToString();
            
        }
    }
}
